package com.yaodian100.ec.creditcard.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yaodian100.core.common.entity.BaseEntity;
import com.yaodian100.core.common.utils.StringUtils;

@Entity
@Table(name = "EC_CREDITCARD_CAMPAIGN_DETAIL")
public class CreditcardDetail extends BaseEntity {

	private static final long serialVersionUID = 1190367926565828595L;

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_ec_creditcard_dtl")
	@SequenceGenerator(name = "seq_ec_creditcard_dtl", sequenceName = "SEQ_EC_CREDITCARD_CAMPAIGN_DTL")
	private long id;

	@ManyToOne
	@JoinColumn(name = "CAMPAIGN_ID")
	private Creditcard creditcard;

	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CREDITCARD_ID", referencedColumnName = "id", unique = true)
	private CreditcardSetting setting;

	@Column(name = "CREDITCARD_PERIOD", length = 100)
	private String creditcardPeriod;

	@Column(name = "CREDITCARD_DETAIL", length = 1000)
	private String creditcardDetail;

	@Column(name = "IS_DELETE", nullable = false)
	private boolean delete = false;

	/**
	 * 该银行所支援的期数
	 */
	@Transient
	private List<String> periods;
	@Transient
	private List<String> fees;
	@Transient
	private List<String> downLimits;
	@Transient
	private List<String> upLimits;

	@Transient
	private Map<String, String> periodMap;
	@Transient
	private Map<String, String> feeMap;
	@Transient
	private Map<String, String> downLimitMap;
	@Transient
	private Map<String, String> upLimitMap;

	/**
	 * 该银行每种期数所对映的手续费(%) key: 期数 value: 手续费(%)
	 */
	@Transient
	private Map<String, Float> periodFees;

	/**
	 * 该银行每种期数所对映的上下限金额 key: 期数 value: List [上限金额, 下限金额]
	 */
	@Transient
	private Map<String, List<Float>> amountLimits;

	@Transient
	private String detailId;

	public Map<String, String> getDownLimitMap() {
		try {
			if (!StringUtils.isBlank(creditcardDetail)) {
				downLimitMap = new HashMap<String, String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();
					String downLimit = detailJO.getJSONArray(period).get(1).toString();
					downLimitMap.put(period, downLimit);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return downLimitMap;
	}

	public Map<String, String> getUpLimitMap() {
		try {
			if (!StringUtils.isBlank(creditcardDetail)) {
				upLimitMap = new HashMap<String, String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();
					String upLimit = detailJO.getJSONArray(period).get(2).toString();
					upLimitMap.put(period, upLimit);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return upLimitMap;
	}

	public Map<String, String> getFeeMap() {
		try {
			if (!StringUtils.isBlank(creditcardDetail)) {
				feeMap = new HashMap<String, String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();
					String fee = detailJO.getJSONArray(period).get(0).toString();
					feeMap.put(period, fee);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return feeMap;
	}

	public List<String> getFees() {
		try {
			if (!StringUtils.isBlank(creditcardDetail)) {
				fees = new ArrayList<String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();

					String fee = detailJO.getJSONArray(period).get(0).toString();
					fees.add(fee);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return fees;
	}

	public List<String> getDownLimits() {
		try {
			if (!StringUtils.isBlank(creditcardDetail)) {
				downLimits = new ArrayList<String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();

					String downLimit = detailJO.getJSONArray(period).get(1).toString();
					downLimits.add(downLimit);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return downLimits;
	}

	public List<String> getUpLimits() {
		try {
			if (!StringUtils.isBlank(creditcardDetail)) {
				upLimits = new ArrayList<String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();

					String upLimit = detailJO.getJSONArray(period).get(2).toString();
					upLimits.add(upLimit);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return upLimits;
	}

	public Map<String, List<Float>> getAmountLimits() {
		try {
			if (!StringUtils.isBlank(creditcardPeriod) && !StringUtils.isBlank(creditcardDetail)) {
				amountLimits = new HashMap<String, List<Float>>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();

					float downLimit = NumberUtils.toFloat(detailJO.getJSONArray(period).get(1).toString(), 0);
					float upLimit = NumberUtils.toFloat(detailJO.getJSONArray(period).get(2).toString(), 0);
					List<Float> limit = new ArrayList<Float>();
					limit.add(new Float(downLimit));
					limit.add(new Float(upLimit));

					amountLimits.put(period, limit);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return amountLimits;
	}

	public void setAmountLimits(Map<String, List<Float>> amountLimits) {
		this.amountLimits = amountLimits;
	}

	public Map<String, Float> getPeriodFees() {
		try {
			if (!StringUtils.isBlank(creditcardPeriod) && !StringUtils.isBlank(creditcardDetail)) {
				periodFees = new HashMap<String, Float>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				JSONObject detailJO = new JSONObject(creditcardDetail);
				for (int i = 0; i < periodJA.length(); i++) {
					String period = periodJA.get(i).toString();
					float fee = NumberUtils.toFloat(detailJO.getJSONArray(period).get(0).toString(), 0);
					periodFees.put(period, new Float(fee));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return periodFees;
	}

	public void setPeriodFees(Map<String, Float> periodFees) {
		this.periodFees = periodFees;
	}

	public Map<String, String> getPeriodMap() {
		try {
			if (!StringUtils.isBlank(creditcardPeriod)) {
				periodMap = new HashMap<String, String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				for (int i = 0; i < periodJA.length(); i++) {
					periodMap.put(periodJA.get(i).toString(), periodJA.get(i).toString());
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return periodMap;
	}

	public void setPeriodMap(Map<String, String> periodMap) {
		this.periodMap = periodMap;
	}

	public List<String> getPeriods() {
		try {
			if (!StringUtils.isBlank(creditcardPeriod)) {
				periods = new ArrayList<String>();
				JSONArray periodJA = new JSONArray(creditcardPeriod);
				for (int i = 0; i < periodJA.length(); i++) {
					periods.add(periodJA.get(i).toString());
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return periods;
	}

	public void setPeriods(List<String> periods) {
		this.periods = periods;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Creditcard getCreditcard() {
		return creditcard;
	}

	public void setCreditcard(Creditcard creditcard) {
		this.creditcard = creditcard;
	}

	public String getCreditcardPeriod() {
		return creditcardPeriod;
	}

	public void setCreditcardPeriod(String creditcardPeriod) {
		this.creditcardPeriod = creditcardPeriod;
	}

	public String getCreditcardDetail() {
		return creditcardDetail;
	}

	public void setCreditcardDetail(String creditcardDetail) {
		this.creditcardDetail = creditcardDetail;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public CreditcardSetting getSetting() {
		return setting;
	}

	public void setSetting(CreditcardSetting setting) {
		this.setting = setting;
	}

	List<String> toList(String[] args) {
		List<String> list = new ArrayList<String>();

		if (args.length > 0) {
			for (String arg : args) {
				if (!StringUtils.isBlank(arg)) {
					list.add(arg);
				}
			}
		}

		return list;
	}

	public String getDetailId() {
		if (id > 0) {
			detailId = String.valueOf(id);
		}
		return detailId;
	}
}
